package br.com.ledger.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;

import br.com.ledger.containers.components.AbstractBalancete;
import br.com.ledger.model.BalanceteVerificacaoBean;
import br.com.ledger.model.LancamentoBean;
import br.com.ledger.model.RazoneteBean;
import br.com.ledger.utils.JTextFieldLimit;

@SuppressWarnings("serial")
public class TelaRegistroBalancete extends JDialog implements KeyListener {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtNome;
	private JFormattedTextField txtDebito, txtCredito;
	private Object[] row;
	private JButton okButton, cancelButton;
	private JComboBox boxNaturezas;
	private BalanceteVerificacaoBean balancete;
	private RazoneteBean razonete;

	/**
	 * Create the dialog.
	 */
	public TelaRegistroBalancete(BalanceteVerificacaoBean balancete, final Object[] row) {
		this.balancete = balancete;
		this.row = row;
		
		setTitle((row == null) ? "Adicionar" : "Alterar");
		
		setResizable(false);
		setModal(true);
		setBounds(100, 100, 485, 324);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBackground(new Color(245, 245, 245));
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel lblNomenclatura = new JLabel("Nomenclatura:");
		lblNomenclatura.setBounds(12, 25, 147, 16);
		contentPanel.add(lblNomenclatura);
		
		txtNome = new JTextField();
		txtNome.setDocument(new JTextFieldLimit(50));
		txtNome.setBounds(12, 40, 229, 25);
		contentPanel.add(txtNome);
		txtNome.setColumns(10);
		txtNome.addKeyListener(this);
		
		JLabel lblNatureza = new JLabel("Natureza:");
		lblNatureza.setBounds(12, 77, 55, 16);
		contentPanel.add(lblNatureza);
		
		JLabel lblValorDoDbito = new JLabel("Valor do D\u00E9bito:");
		lblValorDoDbito.setBounds(12, 131, 147, 16);
		contentPanel.add(lblValorDoDbito);
		
		JLabel lblValorDoCrdito = new JLabel("Valor do Cr\u00E9dito:");
		lblValorDoCrdito.setBounds(12, 184, 147, 16);
		contentPanel.add(lblValorDoCrdito);
		
		txtDebito = new JFormattedTextField();
		txtDebito.setBounds(12, 148, 229, 25);
		txtDebito.setDocument(new JTextFieldLimit(12));
		txtDebito.addKeyListener(this);
		contentPanel.add(txtDebito);
		
		txtCredito = new JFormattedTextField();
		txtCredito.setBounds(12, 201, 229, 25);
		txtCredito.setDocument(new JTextFieldLimit(12));
		txtCredito.addKeyListener(this);
		contentPanel.add(txtCredito);
		
		boxNaturezas = new JComboBox(AbstractBalancete.naturezas);
		boxNaturezas.setBounds(12, 97, 229, 25);
		contentPanel.add(boxNaturezas);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setBorder(new MatteBorder(1, 0, 0, 0, (Color) Color.GRAY));
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				okButton = new JButton("OK");
				okButton.setBackground(new Color(220, 220, 220));
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						setRegistro();
					}
				});
				okButton.setPreferredSize(new Dimension(80, 35));
				okButton.setActionCommand("OK");
				okButton.setEnabled(false);
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				cancelButton = new JButton("Cancelar");
				cancelButton.setBackground(new Color(220, 220, 220));
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						TelaRegistroBalancete.this.razonete = null;
						dispose();
					}
				});
				cancelButton.setPreferredSize(new Dimension(80, 35));
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		setLocationRelativeTo(balancete);
		
		if (row != null) {
			txtNome.setText(String.valueOf(row[1]));
			boxNaturezas.setSelectedItem(row[2]);
			txtCredito.setVisible(false);
			txtDebito.setVisible(false);
			lblValorDoDbito.setVisible(false);
			lblValorDoCrdito.setVisible(false);
			setSize(485, 284);
			okButton.setEnabled(true);
		}
		
		setVisible(true);
	}
	
	private void setRegistro() {
		if (row == null) {
			razonete = new RazoneteBean(txtNome.getText());
			razonete.setCodigo(-1);
			razonete.setBalanceteVerificacao(balancete.getCodigo());
			razonete.setNatureza(String.valueOf(boxNaturezas.getSelectedItem()));
			double valor = Double.parseDouble(txtDebito.getText().replace(",", ".")) - Double.parseDouble(txtCredito.getText().replace(",", "."));
			LancamentoBean lc = new LancamentoBean(-1, -1, (valor < 0) ? "C" : "D", (valor < 0) ? valor * -1 : valor, "", new Date());
			razonete.adcionarLancamento(lc);
		} else {
			razonete = new RazoneteBean(txtNome.getText());
			razonete.setNatureza(String.valueOf(boxNaturezas.getSelectedItem()));
		}
		dispose();
	}
	
	public RazoneteBean getRazonete() {
		return this.razonete;
	}

	@Override
	public void keyPressed(KeyEvent arg0) {	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		if (row != null) {
			if (!txtNome.getText().trim().equals("")) {
				okButton.setEnabled(true);
				return;
			}
		}
		
		if (txtNome.getText().trim().equals("") || txtCredito.getText().trim().equals("") || txtDebito.getText().trim().equals("") ) {
			okButton.setEnabled(false);
		} else {
			try {
				Double.parseDouble(txtCredito.getText().replace(",", "."));
				Double.parseDouble(txtDebito.getText().replace(",", "."));
				okButton.setEnabled(true);
			} catch (NumberFormatException ex) {
				okButton.setEnabled(false);
			}
		}
	}

	@Override
	public void keyTyped(KeyEvent arg0) { }
}
